package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Classinfo;
import com.grad.why.studentsystem.domain.Project;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.service.ClassinfoService;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/studentManageSystem")
@Controller
public class ClassInfoController {
    @Autowired
    private ClassinfoService classinfoService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private OptionparamsService optionparamsService;

    @RequestMapping("/getClassInfos")
    @ResponseBody
    public Object getClassInfoList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            List<CurrentClassinfo> rows = classinfoService.getClassInfoList(offset, limit, sort, search);
            long total = classinfoService.getClassInfoListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteClassinfo/{ids}")
    @ResponseBody
    public String deleteClassinfo(@PathVariable("ids") String ids){
        try{
            String[] idArray = ids.split("-");
            for(String id : idArray){
                int rows = classinfoService.deleteClassinfoById(Integer.parseInt(id));
                if(rows <= 0){
                    return "FAILED";
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addClassinfo")
    @ResponseBody
    public String addClassinfo(Classinfo classinfo){
        try{
            int maxId = classinfoService.getMaxId() + 1;
            classinfo.setId(maxId);
            classinfo.setStudentcount(0);
            int rows = classinfoService.addClassInfoByEntity(classinfo);
            if(rows <= 0){
                return null;
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateClassinfo")
    @ResponseBody
    public String updateClassinfo(Classinfo classinfo){
        try{
            Integer id = classinfo.getId();
            Classinfo oriClassInfo = classinfoService.getClassInfosByPrimaryKey(id);
            classinfo.setStudentcount(oriClassInfo.getStudentcount());

            if(!classinfo.getClassname().equals(oriClassInfo.getClassname())){
                int classNameIsExist = classinfoService.ClassNameIsExist(classinfo.getClassname());
                if(classNameIsExist > 0){
                    return "FAILED";
                }
            }

            if(oriClassInfo.getProjectid() != classinfo.getProjectid()){
                int n = classinfoService.updateClassinfoHandle(oriClassInfo,classinfo);
                if(n <= 0){
                    return "FAILED";
                }
            }

            int n = classinfoService.updateClassInfoByEntity(classinfo);
            if(n<=0){
                return "FAILED";
            }else{
                return "OK";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadClassInfos")
    @ResponseBody
    public String uploadClassInfos(@RequestParam("file") MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadStudentInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadStudentInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = classinfoService.addClassInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
            //上传学生信息逻辑
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
